<?php

/**
 * leveltest actions.
 *
 * @package    elearning
 * @subpackage leveltest
 * @author     Your name here
 */
class leveltestActions extends sfActions
{
	//for indexSuccess
  public function executeIndex(sfWebRequest $request)
  {
  	$lang_current = sfContext::getInstance()->getUser()->getCulture();
    //get all question in random level
	$this->test_list = TestPeer::getQuestionTest($request->getParameter('courseid',0),$request->getParameter('levelid',0));
	//create array answer with key = question_id
	$this->listAnswer = array();
	$questionId = 0;
	$this->listId = "0";	
	foreach( $this->test_list as $row )
	{
		$questionId = $row->getId();
		$this->listAnswer[$questionId] = TestPeer::getAnswerByQuestionId( $questionId );
		$this->listId = $this->listId  . ',' . $questionId;
	}
	$this->coursename = LevelPeer::getCourseNamebyCourseId($request->getParameter('courseid',0), $lang_current);
	$this->levelname = LevelStudyKitPeer::getLevelNameById($request->getParameter('levelid',0));
  }

	//for showSuccess
  public function executeShow(sfWebRequest $request)
  {
  	$lang_current = sfContext::getInstance()->getUser()->getCulture();
	$this->coursename = LevelPeer::getCourseNamebyCourseId( $request->getParameter('courseid',0), $lang_current);
	$this->levelname = LevelStudyKitPeer::getLevelNameById($request->getParameter('levelid',0));
  	
 	//get array list question id
	$this->liId = explode(',',$request->getParameter('txtlistid'));
	if( !isset($this->liId) )
		$this->ret = 0;
	else
		$this->ret = 1;
	$trueAnswer = 0;
	$yourAnswer = "";
	$textName = ""; //name item in form
	$this->countTrueAnswer = 0;
	$this->countAnswer = 0;
	$this->testId = "";
	//mark for test
	foreach( $this->liId as $answer )
	{
		$this->testId .= "," . $answer;
		if( $answer == 0 ) //default
			continue;
		$textName = 'answer['. $answer . ']';
		if( $request->hasParameter($textName)  )
		{
			$yourAnswer = $request->getParameter($textName);
			//that answer is true?
			$trueAnswer  = TestPeer::chk_TrueAnswerByAnswer( $answer , $yourAnswer );
			if(  $trueAnswer == 1 )
				$this->countTrueAnswer  += 1;
		}
		//count for amount of question
		$this->countAnswer ++;
	}
  }
}
